package com.yike.user.controller;

import com.yike.user.param.BannerParam;
import com.yike.user.service.BannerService;
import com.yike.user.vo.BannerVo;
import lombok.extern.slf4j.Slf4j;
import com.yike.user.param.BannerPageParam;
import com.yike.common.base.BaseController;
import com.yike.common.api.ApiResult;
import com.yike.common.pagination.Paging;
import com.yike.common.log.Module;
import com.yike.common.log.OperationLog;
import com.yike.common.enums.OperationLogType;
import com.yike.common.api.Add;
import com.yike.common.api.Update;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 轮播图表 控制器
 *
 * @author Sakura
 * @since 2024-09-12
 */
@Slf4j
@RestController
@RequestMapping("/banner")
@Module("user")
@Api(value = "轮播图API", tags = {"轮播图管理"})
public class BannerController extends BaseController {

    @Autowired
    private BannerService bannerService;

    /**
     * 添加轮播图表
     */
    @PostMapping("/add")
    @OperationLog(name = "添加轮播图", type = OperationLogType.ADD)
    @ApiOperation(value = "添加轮播图", response = ApiResult.class)
    public ApiResult<Boolean> addBanner(@Validated(Add.class) @RequestBody BannerParam bannerParam) throws Exception {
        boolean flag = bannerService.saveBanner(bannerParam);
        return ApiResult.result(flag);
    }

    /**
     * 修改轮播图表
     */
    @PostMapping("/update")
    @OperationLog(name = "修改轮播图", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改轮播图", response = ApiResult.class)
    public ApiResult<Boolean> updateBanner(@Validated(Update.class) @RequestBody BannerParam bannerParam) throws Exception {
        boolean flag = bannerService.updateBanner(bannerParam);
        return ApiResult.result(flag);
    }

    /**
     * 删除轮播图表
     */
    @PostMapping("/delete/{id}")
    @OperationLog(name = "删除轮播图", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除轮播图", response = ApiResult.class)
    public ApiResult<Boolean> deleteBanner(@PathVariable("id") Long id) throws Exception {
        boolean flag = bannerService.deleteBanner(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取轮播图表详情
     */
    @GetMapping("/info/{id}")
    //@OperationLog(name = "轮播图详情", type = OperationLogType.INFO)
    @ApiOperation(value = "轮播图详情", response = BannerVo.class)
    public ApiResult<BannerVo> getBanner(@PathVariable("id") Long id) throws Exception {
        BannerVo bannerVo = bannerService.getBannerById(id);
        return ApiResult.ok(bannerVo);
    }

    /**
     * 轮播图表分页列表
     */
    @PostMapping("/getPageList")
    //@OperationLog(name = "轮播图表分页列", type = OperationLogType.PAGE)
    @ApiOperation(value = "轮播图表分页列", response = BannerVo.class)
    public ApiResult<Paging<BannerVo>> getBannerPageList(@Validated @RequestBody BannerPageParam bannerPageParam) throws Exception {
        Paging<BannerVo> paging = bannerService.getBannerPageList(bannerPageParam);
        return ApiResult.ok(paging);
    }

}

